CssJsHtml

vue.js 路由独享守卫,组件守卫

2019/01/10 09:33 5597 次阅读王梓
★ 打赏
✸ ✸ ✸

main.js路由独享守卫:

    path: '/menu', name: "menulink", component: Menu, beforeEnter: (to, from, next) => {
      alert("非登录状态不能进入此页面!");
      next('/login');
    }

main.js后置钩子:

//后置钩子
// router.afterEach((to, from) => {
//   alert('after each ')
// })

组件守卫Admin.vue:

<template>
      <h1>{{name }}</h1>

</template>

<script>
    export default {
      data(){
        return{
          name:"brownwang"
        }
      },
      beforeRouteEnter:((to, from, next) =>{
        // alert("hello "+this.name);
        // next();
        next((vm => {
          alert("hello "+vm.name);
        }))
      }),
      beforeRouteLeave:((to, from, next) => {
        if(confirm("确定离开?")==true){
          next()
        }else{
          next(false)
        }
      })
    }
</script>

<style scoped>

</style>

✸ ✸ ✸

📜 版权声明

本文作者:王梓 | 原文链接:https://www.bthlt.com/note/203-vue.js 路由独享守卫,组件守卫

出处:葫芦的运维日志 | 转载请注明出处并保留原文链接

📜 留言板

留言提交后需管理员审核通过才会显示